/****************************************************************************************************************************
 * @File name:battery.c
 * @Author:sunshu
 * @Version:v2.00
 * @Date:2017/12/08
 * @Description:get the the percentage and level of the battery capacity remained with VBAT voltage
 * @Others:
 * @Function List:
 1.unsigned char GetBatLevel(unsigned short value):get the percentage of the battery capacity remained with the VBAT voltage
 2.unsigned short GetBatVoltage(adc_interface *pAdc):get the VBAT voltage
 3.unsigned char BatDisplayLevel(adc_interface *pAdc):get the level of the battery capacity remained
 * @History:
****************************************************************************************************************************/

#include "stm32f4xx_hal.h"
#include "battery.h"
#include "main.h"

const static unsigned short BAT_TABLE[][2] = {
    4190,100,
    4100,95,
    4060,90,
    4020,85,
    3980,80,
    3950,75,
    3920,70,
    3895,65,
    3870,60,
    3845,55,
    3820,50,
    3805,45,
    3790,40,
    3780,35,
    3770,30,
    3755,25,
    3740,20,
    3710,15,
    3680,10,
    3450,0,
};

/****************************************************************************************************************************
* @Functions:GetBatLevel()
* @Description:get the percentage of the battery capacity remained with the VBAT voltage
* @Input:value:VBAT voltage
* @Output:
* @Return:the percentage of the battery capacity remained
* @Others:
	VBAT>=4190mv --> 100%
	VBAT<3450mv  --> 0%
* @History:
****************************************************************************************************************************/
unsigned char GetBatLevel(unsigned short value)
{
    unsigned char i;
    unsigned char ret;

    ret = 0;

    for(i=0; i<sizeof(BAT_TABLE)/4; i++)
    {

        if(value >= BAT_TABLE[i][0])
        {
            ret = BAT_TABLE[i][1];
            break;
        }
    }
    return ret;
}

/****************************************************************************************************************************
* @Functions:GetBatVoltage()
* @Description:read ADC_IN4 value and get the VBAT voltage
* @Input:pAdc:the address of the UserAdc
* @Output:
* @Return:VBAT voltage
* @Others:
* @History:
****************************************************************************************************************************/
unsigned short GetBatVoltage(adc_interface *pAdc)
{
    unsigned int Voltage=0;

    pAdc->GetAdc(ADC1_AD_Value);
    Voltage = ADC1_AD_Value[0]*((BAT_PULLUP_RES+BAT_PULLDOWN_RES)/BAT_PULLDOWN_RES)*MCUWORK_VOL/ADC_MAX;					//Voltage*2*3300/65520

    return (unsigned short)Voltage;
}

/****************************************************************************************************************************
* @Functions:BatDisplayLevel()
* @Description:get the level of the battery capacity remained
* @Input:the address of the UserAdc
* @Output:
* @Return:the level of the battery capacity remained
* @Others:
* @History:
****************************************************************************************************************************/
unsigned char BatDisplayLevel(adc_interface *pAdc)
{
    unsigned char per;

    per = GetBatLevel(GetBatVoltage(pAdc));

    if(per > 85)
    {
        return 5;
    }
    if(per > 70)
    {
        return 4;
    }
    if(per > 50)
    {
        return 3;
    }
    if(per > 30)
    {
        return 2;
    }
    if(per > 10)
    {
        return 1;
    }
    return 0;
}

